﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace iTunesFastForward
{
    class ShortcutsManager
    {
        iTunesFastForwardForm _form;
        public ShortcutsManager(iTunesFastForwardForm form)
        {
            _form = form;
        }

        public Dictionary<string,Shortcut> GetAllShortcuts()
        {
            Dictionary<string,Shortcut> scs = new Dictionary<string,Shortcut>();
            // lire l'xml
            XDocument doc = XDocument.Load(@"ShortcutList.xml");

            foreach (XElement node in doc.Elements().First().Elements())
	        {
                Shortcut sc = new Shortcut();
                if (node.Name == "shortcut")
                {

                    string name = node.Elements("name").First().Value;

                    // si c'est dans le fichier de conf, récupérer la valeur
                    if (iTFFSettings.Instance.GetShortCut(name, sc) != null)
                        scs.Add(name, sc);
                    else
                    {
                        // récupérer ce qu'il faut
                        sc.Name = name;
                        sc.Representation = node.Elements("text").First().Value;
                        scs.Add(name, sc);
                    }
                }

                else if (node.Name == "separator")
                {
                    sc.HasSeparatorBefore = true;
                }

	        }

            return scs;
            
        }


        public void DisposeShortcuts(Shortcut[] sc)
        {
            foreach (var s in sc)
                s.UnregisterShortCut();
        }

        /// <summary>
        /// Sets and applies a shortcut
        /// </summary>
        /// <param name="sc"></param>
        /// <param name="handle"></param>
        public void SetShortcut(Shortcut sc, IntPtr handle)
        {
            // sauvegarder
            iTFFSettings.Instance.SetShortCut(sc);

            // appliquer
            RegisterShortcut(sc, handle);
        }

        public void UnsetShortcut(Shortcut sc)
        {
            // supprimer de la sauvegarde
            iTFFSettings.Instance.UnsetShortCut(sc);

            // desenregistrer
            UnregisterShortcut(sc);
        }


        #region Register and unregister shortcut

        /// <summary>
        /// Register a shortcut
        /// </summary>
        /// <param name="sc"></param>
        public void RegisterShortcut(Shortcut sc, IntPtr handle)
        {
            if (sc.Keys == Keys.None)
                return;

            try
            {
                sc.Handle = handle;

                sc.RegisterShortCut();

                sc.KeyPressed += new EventHandler(sc_KeyPressed);

            }
            catch (Exception)
            {
                UnregisterShortcut(sc);
                throw;
            }
        }



        /// <summary>
        /// Delete the shortcut
        /// </summary>
        /// <param name="sc"></param>
        public void UnregisterShortcut(Shortcut sc)
        {
            sc.UnregisterShortCut();
        }

        #endregion


        #region Action

        void sc_KeyPressed(object sender, EventArgs e)
        {
            DoAction(((Shortcut)sender).Name);
        }

        /// <summary>
        /// Does an action on key pressed
        /// </summary>
        void DoAction(string actionName)
        {

            if (!iTunesActions.Instance.COMCallDisabled)
            {
                switch (actionName)
                {
                    case "VolumeUp":
                        iTunesActions.Instance.Volume += 5;
                        break;
                    case "VolumeDown":
                        iTunesActions.Instance.Volume -= 5;
                        break;
                    case "Mute":
                        iTunesActions.Instance.ToggleMute();
                        break;
                    case "ToggleShuffle":
                        iTunesActions.Instance.ToggleShuffle();
                        break;
                    case "ShowToast":
                        _form.ShowToast(iTunesActions.Instance.PlayingTrack, true);
                        break;
                    case "ShowLyrics":
                        _form.LyricsDownloadAndApply(iTunesActions.Instance.PlayingTrack, false);
                        _form.LyricsShow();
                        break;
                    case "SearchTags":
                        _form.MusicDNSSearchTagsForSelectedTrack();
                        break;
                    case "AddToPlaylist":
                        _form.AddSelectedSongToPlaylist();
                        break;
                    case "SongRatingClear":
                        iTunesActions.Instance.SongRate = 0;
                        break;
                    case "SongRating1":
                        iTunesActions.Instance.SongRate = 1;
                        break;
                    case "SongRating2":
                        iTunesActions.Instance.SongRate = 2;
                        break;
                    case "SongRating3":
                        iTunesActions.Instance.SongRate = 3;
                        break;
                    case "SongRating4":
                        iTunesActions.Instance.SongRate = 4;
                        break;
                    case "SongRating5":
                        iTunesActions.Instance.SongRate = 5;
                        break;
                    default:
                        iTunesActions.Instance.DoAction(actionName);
                        break;
                }
            }
        } 
        #endregion

    }
}
